﻿Imports extranet.BE
Imports extranet.BC
Imports System.Data
Imports MetodosComuns
Imports System.Xml
Imports System.Collections.Generic

Partial Class ranking_AdmRnkGerenciamentoRanking
    Inherits System.Web.UI.Page

    Protected Sub btnSalvar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
        Dim objBERanking As New BERanking

        objBERanking.Nome = txtNome.Text
        objBERanking.Planilha = txtPlanilha.Text
        objBERanking.Planilha = objBERanking.Planilha.Replace("\", "\\")
        objBERanking.Fim = New Date(Convert.ToInt32(txtDtFim.Text.Split("/")(2)), Convert.ToInt32(txtDtFim.Text.Split("/")(1)), Convert.ToInt32(txtDtFim.Text.Split("/")(0)))
        objBERanking.Inicio = New Date(Convert.ToInt32(txtDtInicio.Text.Split("/")(2)), Convert.ToInt32(txtDtInicio.Text.Split("/")(1)), Convert.ToInt32(txtDtInicio.Text.Split("/")(0)))

        If objBERanking.Inicio <= objBERanking.Fim Then
            If lblId.Visible = False Then
                objBERanking.IdRanking = ""
            Else
                objBERanking.IdRanking = lblId.Text
            End If

            objBERanking.Premio = txtPremio.Text
            objBERanking.Produto = txtProduto.Text
            objBERanking.Tipo = lblTipo.Text

            Try
                BCRanking.SalvarRanking(objBERanking)
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
                System.Web.HttpContext.Current.Response.Write("alert(""Ranking Salvo com Sucesso!"")" & vbCrLf)
                System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
            Catch ex As ApplicationException
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
                System.Web.HttpContext.Current.Response.Write("alert(""Erro Salvando o Evento: " & ex.Message & """)" & vbCrLf)
                System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
            End Try
        Else
            lblErro.Visible = True
        End If
    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click
        Try
            BCEvento.DeletarEvento(lblId.Text)
            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
            System.Web.HttpContext.Current.Response.Write("alert(""Ranking Excluído com Sucesso!"")" & vbCrLf)
            System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
            Response.Redirect("AdmRnkListarRankings.aspx")
        Catch ex As ApplicationException
            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
            System.Web.HttpContext.Current.Response.Write("alert(""Erro Excluindo o Evento: " & ex.Message & """)" & vbCrLf)
            System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
        End Try
    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click
        Response.Redirect("AdmRnkListarRankings.aspx")
    End Sub

    Protected Sub btnTestar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTestar.Click
        Dim objBERanking As BERanking = BCRanking.SelecionarRanking(lblId.Text)

        grvRankingTeste.DataSource = BCRankeado.LerRankeados(txtPlanilha.Text, False)
        grvRankingTeste.DataBind()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '==================       PROCEDIMENTO  PADRÃO  SEGURANÇA     =============================
        Dim objBESeguranca As BESeguranca
        Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
        If IsNothing(objBEUsuario) Then Response.Redirect(TELA_LOGOUT)
        Session("strTela") = MetodosComuns.GetCurrentPageName()
        If Not IsPostBack Then          ' SEGURANÇA
            objBESeguranca = BCSeguranca.SelecionaPermissoes(objBEUsuario.GrupoAcesso, Session("strTela"))
            If Not objBESeguranca.Autorizado Then Response.Redirect(TELA_LOGOUT)
            Session("objBESeguranca") = objBESeguranca
        Else
            objBESeguranca = Session("objBESeguranca")
        End If

        If Not IsPostBack Then
            Dim strIdRanking As String = Request.QueryString("Ranking")
            If strIdRanking = "" Then
                lblTitulo.Text = "Inclusão de Ranking"
                btnExcluir.Visible = False
                btnSalvar.Visible = True
                btnVoltar.Visible = True
                lblTipo.Text = "produto"
                lblCaptionId.Visible = False
                lblId.Visible = False
            Else
                Dim objBERanking As BERanking = BCRanking.SelecionarRanking(strIdRanking)
                lblTitulo.Text = "Alteração de Ranking"

                lblId.Text = strIdRanking
                txtDtFim.Text = objBERanking.Fim.ToString("dd/MM/yyyy")
                txtDtInicio.Text = objBERanking.Inicio.ToString("dd/MM/yyyy")
                txtNome.Text = objBERanking.Nome
                txtPlanilha.Text = objBERanking.Planilha
                txtPremio.Text = objBERanking.Premio
                txtProduto.Text = objBERanking.Produto
                lblTipo.Text = objBERanking.Tipo

                If lblTipo.Text = "geral" Then
                    btnExcluir.Enabled = False
                End If

                lblCaptionId.Visible = True
                lblId.Visible = True
                btnExcluir.Visible = False
                btnSalvar.Visible = True
                btnVoltar.Visible = True
            End If
        End If
    End Sub
End Class
